package com.maimang.platform.common.service.impl;

import com.maimang.platform.common.entity.SysFileChannel;
import com.maimang.platform.common.entity.SysFilePlay;
import com.maimang.platform.common.mapper.SysFileChannelMapper;
import com.maimang.platform.common.mapper.SysFilePlayMapper;
import com.maimang.platform.common.service.SysFileChannelService;
import com.maimang.platform.exception.BusinessException;
import com.maimang.platform.lang.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysFileChannelServiceImpl
 * @描述 <br>文件通道service实现层</br>
 * @日期 2019-04-25
 * @email jintaozhao@qq.com
 */
@Service
public class SysFileChannelServiceImpl implements SysFileChannelService {

	@Autowired
	private SysFileChannelMapper sysFileChannelMapper;

	@Autowired
	private SysFilePlayMapper sysFilePlayMapper;

	/**
	 * 查询文件通道信息
	 *
	 * @param channelId 文件通道ID
	 * @return 文件通道信息
	 */
	@Override
	public SysFileChannel selectSysFileChannelById(Long channelId) {
		return sysFileChannelMapper.selectSysFileChannelById(channelId);
	}

	/**
	 * 查询文件通道列表
	 *
	 * @param fileChannel 文件通道信息
	 * @return 文件通道集合
	 */
	@Override
	public List<SysFileChannel> selectSysFileChannelList(SysFileChannel fileChannel) {
		return sysFileChannelMapper.selectSysFileChannelList(fileChannel);
	}

	/**
	 * 新增文件通道
	 *
	 * @param fileChannel 文件通道信息
	 * @return 结果
	 */
	@Override
	public int insertSysFileChannel(SysFileChannel fileChannel) {
		return sysFileChannelMapper.insertSysFileChannel(fileChannel);
	}

	/**
	 * 修改文件通道
	 *
	 * @param fileChannel 文件通道信息
	 * @return 结果
	 */
	@Override
	public int updateSysFileChannel(SysFileChannel fileChannel) {
		return sysFileChannelMapper.updateSysFileChannel(fileChannel);
	}

	/**
	 * 删除文件通道信息
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteSysFileChannelByIds(String ids) {
		return sysFileChannelMapper.deleteSysFileChannelByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 删除对应的播放文件
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public int removeFile(String ids) {
		int result = 0;
		String[] idArr = ids.split(",");
		Long id = Long.valueOf(idArr[0]);
		SysFilePlay filePlay = sysFilePlayMapper.selectSysFilePlayById(id);
		if (filePlay == null) {
			throw new BusinessException("文件已被删除，请刷新后重试！");
		}
		result = sysFilePlayMapper.deleteSysFilePlayByIds(ConvertUtils.toStrArray(ids));
		if (result > 0) {
			result = sysFilePlayMapper.updatePayLimit(filePlay.getChannelCode(), result, filePlay.getOrderNum());
		} else {
			throw new BusinessException("文件删除失败！");
		}
		return result;
	}

	@Override
	public int saveLimit(String ids) {
		String[] idArr = ids.split(",");
		Long id = 0l;
		for (int i = 0; i < idArr.length; i++) {
			id = Long.valueOf(idArr[i]);
			SysFilePlay filePlay = sysFilePlayMapper.selectSysFilePlayById(id);
			if (filePlay == null) continue;
			filePlay.setOrderNum(i + 1);
			sysFilePlayMapper.updateSysFilePlay(filePlay);
		}
		return 1;
	}
}
